<template>
    <el-dialog title="添加角色" :visible.sync="showDialog" width="30%">
         <el-form label-width="100px"  class="info" :model="formModel" :rules="rules" ref="roleInfo">
            <el-form-item label="角色名称" prop="name">
                 <el-input size="mini" placeholder="请输入角色名称" v-model="formModel.name"></el-input>
            </el-form-item>
            <el-form-item label="启用">
                <el-switch v-model="formModel.state" :active-value = '1' :inactive-value="0"></el-switch>
            </el-form-item>
            <el-form-item label="角色描述" prop="description">
                 <el-input placeholder="请输入角色描述" size="mini" type="textarea" rows="3" v-model="formModel.description"></el-input>
            </el-form-item>
            <el-form-item>
                <el-row type="flex" justify="end" align="middle">
                    <el-col :span="12" style="padding-left: 8px">
                        <el-button type="primary" size="mini" @click="isOk">确认</el-button>
                        <el-button size="mini" @click="close">取消</el-button>
                    </el-col>
                </el-row>
            </el-form-item>
         </el-form>
    </el-dialog>
</template>

<script>
import { getAddRoleService } from '@/api/role';
import { Message } from 'element-ui';
export default {
  props: {
    showDialog : {
      type: Boolean,
      default: false
    },
    getRoleList: {
        type: Function,
        require: true
    }
  },
  data(){
    return {
        formModel: {
            name: '',
            description: '', //角色描述
            state: 0
        },
        rules: {
            name: [
                // 非空
                { required: true, message: '请输入角色名称', trigger: 'blur' },
            ],
            description: [
                // 非空
                { required: true, message: '请输入角色名称', trigger: 'blur' },
            ]
        }
    }
  },
  methods: {
    close(){
        this.$emit('changeShow')
        this.$refs.roleInfo.resetFields()
    },
   async isOk() {
       await this.$refs.roleInfo.validate()
       await getAddRoleService(this.formModel)
       this.$refs.roleInfo.resetFields()
       this.close()
       this.getRoleList()
       Message({type: 'success', message: '添加成功'})
    }
  }
}
</script>

<style scoped>
.info{
    width: 90%; 
    margin: 0 auto
}
</style>